package com.kainitram.drivebook;

import java.util.HashMap;
import com.kainitram.drivebook.fragments.*;
import com.kainitram.library.sm.*;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;

/**
 * Preference activity for DriveBook
 * 
 * @author Mino develop
 * 
 */
public class DriveBookPreferenceFragment extends PreferenceFragment {

	private HashMap<String, ContentFragmentCreator> mFragmentCreators;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.addPreferencesFromResource(R.xml.drive_book_slide_menu);
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen screen,
			Preference pref) {
		SlidingContentFragment contentFragment = null;
		String title = pref.getTitle().toString();
		DriveBookMainActivity mainActivity = (DriveBookMainActivity) getActivity();
		initFragmentCreators();

		contentFragment = mFragmentCreators.get(title)
				.getContentFragmentInstance();

		if (contentFragment != null) {
			mainActivity.switchContent(contentFragment);
		}

		return true;
	}

	private void initFragmentCreators() {

		if (mFragmentCreators != null) {
			return;
		}

		mFragmentCreators = new HashMap<String, ContentFragmentCreator>();

		mFragmentCreators.put(getString(R.string.print),
				(new FragmentCreators().new PrintingFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.export),
				(new FragmentCreators().new ExportFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.home),
				(new FragmentCreators().new HomeFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.loggin),
				(new FragmentCreators().new LogginFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.drives),
				(new FragmentCreators().new DrivesListFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.planning),
				(new FragmentCreators().new PlanningFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.coast),
				(new FragmentCreators().new CoastsListFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.cars),
				(new FragmentCreators().new CarsListFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.partners),
				(new FragmentCreators().new PartnersListFragmentCreator()));
		
		mFragmentCreators.put(getString(R.string.passengers),
				(new FragmentCreators().new PassengersListFragmentCreator()));
	}

}
